window.onload=function(){
	$(".maintaininfos").css("backgroundColor","#224247").siblings().css("backgroundColor","#365d62");
	//搜索
	$(".queryBtn").click(function(){
		var device_code = $(".deviceCodeBox").val();
		if(device_code=="%"){
			hint("非法输入，请正确输入编号！");
		}else{
			window.location.href="/admin/device/maintainInfos?condition="+device_code;
		}
	})
	//链接
	$(".userNameCell").click(function(){
		window.location.href="/admin/device/maintainHistory?customer_name="+$(this).html();
	});
	// 添加
	$(".addMaintain_infos").click(function(){
		//清空输入框文本
		$(".addUserName").val("");
		$(".addUserPhone").val("");
		$(".addUserAddress").val("");
		$(".addDevice_Code").val("");
		$(".addServicePart").val("");
		$(".addChargeAmount").val("");
		$(".addServiceMen").val("");
		$(".addServiceMenTel").val("");
		$("#addBespeakDate").val("");
		$(".labelBox").html("");
		$(".other_addText").val("");
		$("#addLabelBox").html("");
		//设置显示/隐藏弹框
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['添加维护信息', 'font-size:18px;'],
				area: ['800px', '500px'],
			  	content: $(".addHintBox"),
			}); 
		});
		// 点击取消
		$(".addCancelBox").unbind('click').click(function(){
			window.location.href="/admin/device/maintainInfos";
		});
		// 点击确定
		$(".addOkBox").unbind('click').click(function(){
			if($(".addUserName").val()==""){
				$("#addLabelBox").html("请输入用户名。");
			}else if($(".addUserPhone").val()==""){
				$("#addLabelBox").html("请输入用户手机号码。");
			}else if($(".addUserAddress").val()==""){
				$("#addLabelBox").html("请输入安装地址。");
			}else if($(".addUserPhone").val()!=""&!(/^1[34578]\d{9}$/.test($(".addUserPhone").val()))){
				$("#addLabelBox").html("手机号码输入有误，请重新输入！");
			}else if($(".addDevice_Code").val()==""){
				$("#addLabelBox").html("请输入16位设备编号");
			}else if($(".addDevice_Code").val()!=""&!(/^[0-9]{16}$/.test($(".addDevice_Code").val()))){
				$("#addLabelBox").html("输入有误，请输入16位设备编号！");
			}else if($("#addBespeakDate").val()==""){
				$("#addLabelBox").html("请输入预约日期");
			}else{
				$("#addLabelBox").html("");
				$.ajax({
					url:"/admin/api/device/addMaintainInfo",
					type:"post",
					dataType:"json",
					data:{
						device_code:$(".addDevice_Code").val(),
			            customer_name:$(".addUserName").val(),
			            phone:$(".addUserPhone").val(),
			            installation_address:$(".addUserAddress").val(),
			            charge:$(".addChargeAmount").val(),
			            operator:$(".addServiceMen").val(),
			            operator_phone:$(".addServiceMenTel").val(),
			            component:$(".addServicePart").val(),
			            maintain_at:$("#addMaintenanceDate").val(),
			            reserve_at:$("#addBespeakDate").val(),
			            remark:$(".other_addText").val()
					},
					success:function(data){
						if(data.status.errCode==0){
							window.location.href="/admin/device/maintainInfos"; //添加成功时候刷新页面
						}else{
							$("#addLabelBox").html(data.status.message);
						}
					}
				})
			}	
		});
	});
	// 编辑
	$(".writeBtn").click(function(){
		$("#labelBox").html("");
		var _this=$(this);
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['编辑安装信息', 'font-size:18px;'],
				area: ['800px', '500px'],
			  	content: $(".editHintBox"),
			}); 
		});
		$(".userName").val(_this.parent().parent().siblings(".userNameCell").html());
		$(".userPhone").val(_this.parent().parent().siblings(".userPhoneCell").html());
		$(".userAddress").val(_this.parent().parent().siblings(".userAddressCell").html());
		$(".device_Code").val(_this.parent().parent().siblings(".deviceCodeCell").html());
		$(".servicePart").val(_this.parent().parent().siblings(".servicePartCell").html());
		$(".chargeAmount").val(_this.parent().parent().siblings(".paymentCell").html());
		$(".serviceMen").val(_this.parent().parent().siblings(".serviceMenCell").html());
		$(".serviceMenTel").val(_this.parent().parent().siblings(".serviceMenTelCell").html());
		$("#editBespeakDate").val(_this.parent().parent().siblings(".YMDH").html());
		$("#editMaintenanceDate").val(_this.parent().parent().siblings(".YMD").html());
		$(".other_editMText").val(_this.parent().parent().siblings(".MemoText").html());
		//取消按钮
		$(".noBox").unbind('click').click(function(){
			window.location.reload();
		})
		//确定按钮
		$(".okBox").unbind('click').click(function(){
			if($(".userName").val()==""){
				$("#labelBox").html("用户名不能为空！");
			}else if(!(/^1[34578]\d{9}$/.test($(".userPhone").val()))){
				$("#labelBox").html("请正确输入用户手机号码！");
			}else if($(".userAddress").val()==""){
				$("#labelBox").html("安装地址不能为空！");
			}else if($(".device_Code").val()==""){
				$("#labelBox").html("设备编号不能为空！");
			}else if($(".servicePart").val()==""){
				$("#labelBox").html("维修部件不能为空！");
			}else if($(".chargeAmount").val()==""){
				$("#labelBox").html("请输入缴费金额！");
			}else if($(".serviceMen").val()==""){
				$("#labelBox").html("请输入维护人！");
			}else if(!(/^1[34578]\d{9}$/.test($(".serviceMenTel").val()))){
				$("#labelBox").html("请正确输入维护人手机号码！");
			}else if($("#editBespeakDate").val()==""){
				$("#labelBox").html("预约日期不能为空！");
			}else if($("#editMaintenanceDate").val()==""){
				$("#labelBox").html("维修日期不能为空！");
			}else{
				$("#labelBox").html("");
				$.ajax({
					url:"/admin/api/device/updateMaintainInfo",
					type:"post",
					dataType:"json",
					data:{
						id:_this.siblings(".writeBtnIdVal").val(),
						device_code:$(".device_Code").val(),
			            customer_name:$(".userName").val(),
			            phone:$(".userPhone").val(),
			            installation_address:$(".userAddress").val(),
			            charge:$(".chargeAmount").val(),
			            operator:$(".serviceMen").val(),
			            operator_phone:$(".serviceMenTel").val(),
			            component:$(".servicePart").val(),
			            maintain_at:$("#editMaintenanceDate").val(),
			            reserve_at:$("#editBespeakDate").val(),
			            remark:$(".other_editMText").val()
					},
					success:function(data){
						if(data.status.errCode==0){
							_this.parent().parent().siblings(".userNameCell").html($(".userName").val());
							_this.parent().parent().siblings(".userPhoneCell").html($(".userPhone").val());
							_this.parent().parent().siblings(".userAddressCell").html($(".userAddress").val());
							_this.parent().parent().siblings(".deviceCodeCell").html($(".device_Code").val());
							_this.parent().parent().siblings(".servicePartCell").html($(".servicePart").val());
							_this.parent().parent().siblings(".paymentCell").html($(".chargeAmount").val());
							_this.parent().parent().siblings(".serviceMenCell").html($(".serviceMen").val());
							_this.parent().parent().siblings(".serviceMenTelCell").html($(".serviceMenTel").val());
							_this.parent().parent().siblings(".YMDH").html($("#editBespeakDate").val());
							_this.parent().parent().siblings(".YMD").html($("#editMaintenanceDate").val());
							_this.parent().parent().siblings(".MemoText").html($(".other_editMText").val());
							$("#labelBox").html("操作成功");
							// window.location.href="/admin/device/maintainInfos"; //编辑成功后刷新页面
						}else{
							$("#labelBox").html(data.status.message);
						}
					}
				})
			}
		});
	});
	// 删除
	$(".deleteBtn").click(function(){
		$(".deleteLabelMBox").html("");
		var _this=$(this);
		layui.use('layer', function(){
		  	var layer = layui.layer;
		  	layer.open({
				type: 1, 
				title: ['删除此条维护信息', 'font-size:18px;'],
				area: ['500px', '300px'],
			  	content: $(".deleteMBtn"),
			}); 
		});
		//取消按钮
		$(".noBtn").unbind('click').click(function(){
			window.location.reload();
		})
		//确定按钮
		$(".okBtn").unbind('click').click(function(){
			$.ajax({
				url:"/admin/api/device/deleteMaintainInfo",
				type:"post",
				dataType:"json",
				data:{
					id:_this.siblings(".deleteBtnIdVal").val()
				},
				success:function(data){
					if(data.status.errCode==0){
						_this.parent().parent().parent().remove();
						$(".deleteLabelMBox").html("此条记录已成功删除");
					}else{
						$(".deleteLabelMBox").html(data.status.message);
					}
				}
			})
		})
	})
	//勾选框
	$(".checkedBox").click(function(){
		$(".reminderLabelBox").html("");
		var _this=$(this);
		if(!$(this).hasClass("checkedBoxBg")){
			layui.use('layer', function(){
			  	var layer = layui.layer;
			  	layer.open({
					type: 1, 
					title: ['是否已处理', 'font-size:18px;'],
					area: ['500px', '300px'],
				  	content: $(".checkMHintBox"),
				}); 
			});
			//取消按钮
			$(".noLBtn").unbind('click').click(function(){
				window.location.reload();
			})
			//确定按钮
			$(".yesRBtn").unbind('click').click(function(){
				$.ajax({
					url:"/admin/api/device/processMaintainInfo",
					type:"post",
					dataType:"json",
					data:{
						id:_this.siblings("input").val(),
						status:1
					},
					success:function(data){
						if (data.status.errCode==0) {
							_this.addClass("checkedBoxBg");
							$(".notMaintaininfosNum").html(data.data.count);
							$(".reminderLabelBox").html("已成功处理");
						}else{
							$(".reminderLabelBox").html(data.status.message);
						}
					}
				})
			});
		}
	});
	//日期控件
	layui.use('laydate', function(){
	  	var element =laydate = layui.laydate
	})
}